package cn.ctyun.videoplayer.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SharePreferencesUtils {
    private SharedPreferences mSharedPreferences;

    public SharePreferencesUtils(Context ctx, String name) {
        if (null != ctx) {
            this.mSharedPreferences = ctx.getSharedPreferences(name, 0);
        }
    }

    @SuppressLint({"CommitPrefEdits"})
    public int increase(String key) {
        if (null != key && !"".equals(key)) {
            int counter = this.mSharedPreferences.getInt(key, 0);
            ++counter;
            Editor editor = this.mSharedPreferences.edit();
            editor.putInt(key, counter);
            editor.apply();
            return counter;
        } else {
            return 0;
        }
    }

    public void clear() {
        Editor editor = this.mSharedPreferences.edit();
        editor.clear();
        editor.apply();
    }

    @SuppressLint({"CommitPrefEdits"})
    public int decrease(String key) {
        if (null != key && !"".equals(key)) {
            int counter = this.mSharedPreferences.getInt(key, 0);
            --counter;
            Editor editor = this.mSharedPreferences.edit();
            editor.putInt(key, counter);
            editor.apply();
            return counter;
        } else {
            return 0;
        }
    }

    @SuppressLint({"CommitPrefEdits"})
    public void setLongValue(String key, long value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putLong(key, value);
            editor.apply();
            editor = null;
        }
    }

    public void setLongValueSync(String key, long value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putLong(key, value);
            editor.commit();
        }
    }

    public void setIntValue(String key, int value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putInt(key, value);
            editor.apply();
            editor = null;
        }
    }

    public int getIntValue(String key) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getInt(key, -1) : 0;
    }

    public int getIntValue(String key, int defaultValue) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getInt(key, defaultValue) : 0;
    }

    public long getLongValue(String key) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getLong(key, -1L) : 0L;
    }

    public long getLongValue(String key, long defaultValue) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getLong(key, defaultValue) : 0L;
    }

    public void setBoolean(String key, boolean value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putBoolean(key, value);
            editor.apply();
            editor = null;
        }
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getBoolean(key, defaultValue) : defaultValue;
    }

    public void setStringValue(String key, String value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putString(key, value);
            editor.apply();
            editor = null;
        }
    }

    public String getStringValue(String key) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getString(key, "") : "";
    }

    public String getStringValue(String key, String defaultValue) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getString(key, defaultValue) : "";
    }

    @SuppressLint({"CommitPrefEdits"})
    public void setCounterValue(String key, int value) {
        if (null != key && !"".equals(key)) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putInt(key, value);
            editor.apply();
            editor = null;
        }
    }

    public int getCounter(String key) {
        return null != key && !"".equals(key) ? this.mSharedPreferences.getInt(key, -1) : -1;
    }

    @SuppressLint({"CommitPrefEdits"})
    public void resetCounter(String key) {
        if (null != key && 0 != key.length()) {
            Editor editor = this.mSharedPreferences.edit();
            editor.putInt(key, 0);
            editor.apply();
        }
    }

    @SuppressLint({"CommitPrefEdits"})
    public void resetCounter(String[] keys) {
        if (null != keys && 0 != keys.length) {
            Editor editor = this.mSharedPreferences.edit();
            boolean hashChange = false;
            String[] var4 = keys;
            int var5 = keys.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String key = var4[var6];
                if (null != key && !"".equals(key)) {
                    editor.putInt(key, 0);
                    hashChange = true;
                }
            }

            if (hashChange) {
                editor.apply();
            }

        }
    }

    public void setSerializableObject(String key, Object object) throws IOException {
        Editor spEdit = this.mSharedPreferences.edit();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(object);
        String bytesToHexString = ConvertUtils.bytesToHexString(bos.toByteArray());
        spEdit.putString(key, bytesToHexString);
        spEdit.commit();
    }

    public Object getSerializableObject(String key) throws IOException, ClassNotFoundException {
        if (TextUtils.isEmpty(key)) {
            return null;
        } else {
            String string = this.mSharedPreferences.getString(key, "");
            if (TextUtils.isEmpty(string)) {
                return null;
            } else {
                byte[] stringToBytes = ConvertUtils.hexStringToBytes(string);
                ByteArrayInputStream bis = new ByteArrayInputStream(stringToBytes);
                ObjectInputStream is = new ObjectInputStream(bis);
                Object readObject = is.readObject();
                return readObject;
            }
        }
    }
}